require(ggplot2)
require(gridExtra)
require(scales)

## MANIFESTO PROJECT
## Download from https://manifestoproject.wzb.eu/
MPDataset_MPDS2014b <- read.csv("MPDataset_MPDS2014b.csv", # Not provided
                                encoding = "latin1", 
                                header = TRUE, 
                                stringsAsFactors = FALSE)

# View(subset(MPDataset_MPDS2014b, countryname=='Italy'))

rile_expr = quote((per104 + per201 + per203 + per305 + per401 + per402 + per407 + per414 + per505 + per601 + per603 + per605 + per606) - (per103 + per105 + per106 + per107 + per403 + per404 + per406 + per412 + per413 + per504 + per506 + per701 + per202))
planeco_expr = quote(per403 + per404 + per412)
markeco_expr = quote(per401 + per414)
welfare_expr = quote(per503 + per504)
intpeace_expr = quote(per102 + per105 + per106)

df <- data.frame(party=subset(MPDataset_MPDS2014b, countryname=='Italy')$party,
                 partyname=subset(MPDataset_MPDS2014b, countryname=='Italy')$partyname,
                 election=as.Date(subset(MPDataset_MPDS2014b, countryname=='Italy')$edate, format="%d/%m/%Y"),
                 rile=with(subset(MPDataset_MPDS2014b, countryname=='Italy'), eval(rile_expr)),
                 planeco=with(subset(MPDataset_MPDS2014b, countryname=='Italy'), eval(planeco_expr)),
                 markeco=with(subset(MPDataset_MPDS2014b, countryname=='Italy'), eval(markeco_expr)),
                 welfare=with(subset(MPDataset_MPDS2014b, countryname=='Italy'), eval(welfare_expr)),
                 intpeace=with(subset(MPDataset_MPDS2014b, countryname=='Italy'), eval(intpeace_expr)))

m5s_position <- list()
m5s_position[['rile']] <- df$rile[df$party==32956]
m5s_position[['planeco']] <- df$planeco[df$party==32956]
m5s_position[['markeco']] <- df$markeco[df$party==32956]
m5s_position[['welfare']] <- df$welfare[df$party==32956]
m5s_position[['intpeace']] <- df$intpeace[df$party==32956]

# ggplot(df, aes(x=election, y=position, colour=as.factor(party), group=as.factor(party))) + 
#   geom_point() + geom_line() + guides(colour=FALSE) + 
#   geom_dl(aes(label=as.factor(partyname)),
#           method="top.bumpup")

# focus_on <- c('32220', '32320', '32520', '32710', '32210', '32110', '32212', '32720', '32710', '32956')
# focus_on <- c('32220', '32520')
# 
# ggplot(subset(df, party %in% focus_on), aes(x=election, y=position, colour=as.factor(party), group=as.factor(party))) + 
#   geom_point() + guides(colour=FALSE) + 
#   geom_dl(aes(label=as.factor(partyname)),
#           method="top.bumpup") +
#   geom_smooth(se=FALSE)


# Plot function
plotPosition <- function(i, titles, dimension){
  if (dimension==TRUE) {
    df <- subset(df, get(i)!=0)
  }
  xintercept1 <- with(df, median(get(i)))
  xintercept2 <- m5s_position[[i]]
  ypos <- with(df, max(density(get(i))$y)) / 2
  p <- ggplot(df) + geom_density(aes_string(x=i)) + 
    geom_vline(xintercept=xintercept1, linetype="dotted") +
    annotate("text", x = xintercept1, y = ypos / 2, label = "Median", size=2) +
    labs(x=paste0(titles[[i]]," (n=",nrow(df),")"))
  if (xintercept2!=0) {
    p <- p + geom_vline(xintercept=xintercept2, color='red', linetype="dotted") +
      annotate("text", x = xintercept2, y = ypos, label = "M5S", color='red', size=2)
  }
  return(p)
}

titles <- list()
titles[['rile']] <- 'Right-left position'
titles[['planeco']] <- 'Planned economy'
titles[['markeco']] <- 'Market economy'
titles[['welfare']] <- 'Welfare'
titles[['intpeace']] <- 'International peace'

p1 <- plotPosition('rile', titles, dimension=FALSE)
p2 <- plotPosition('planeco', titles, dimension=FALSE)
p3 <- plotPosition('markeco', titles, dimension=FALSE)
p4 <- plotPosition('welfare', titles, dimension=FALSE)

grid.arrange(p1, arrangeGrob(p2,p3,p4, ncol=3), heights=c(2.5/4, 1.5/4), ncol=1)

